#include "Player.h"

Player::Player(): x(400), y(300), speed(200)
{
	playerTexture.loadFromFile("Assets/Player.png");
	playerSprite.setTexture(playerTexture);
	playerSprite.setScale(sf::Vector2f(0.2,0.2));
	playerSprite.setOrigin(125,150);
	mousePos = sf::Mouse::getPosition();
	health = 100;
	playerTexture.setSmooth(true);
}

sf::Texture Player::getplayerTexture()
{
	return playerTexture;
}

sf::Sprite Player::getplayerSprite()
{
	return playerSprite;
}

void Player::movePlayer(direction dir, sf::Time& delta)
{
	
	switch(dir)
	{
	case direction::LEFT:
		x -= speed*delta.asSeconds();
		//playerSprite.setRotation(270);
		break;

	case direction::RIGHT:
		x += speed*delta.asSeconds();
		//playerSprite.setRotation(90);
		break;



	case direction::UP:
		y -= speed*delta.asSeconds();
		//playerSprite.setRotation(0);
		break;



	case direction::DOWN:
		y += speed*delta.asSeconds();
		//playerSprite.setRotation(180);
		break;
	}

}

void Player::updatePlayer()
{
	playerSprite.setPosition(x, y);
	
}

void Player::playerLook(sf::RenderWindow& wnd)
{
	sf::Vector2f curPos = playerSprite.getPosition();
    sf::Vector2i position = sf::Mouse::getPosition(wnd);

    // now we have both the sprite position and the cursor
    // position lets do the calculation so our sprite will
    // face the position of the mouse
    const float PI = 3.14159265;

    float dx = curPos.x - position.x;
    float dy = curPos.y - position.y;

    float rotation = (atan2(dy, dx)) * 180 / PI - 90;

    playerSprite.setRotation(rotation);
}

void Player::inputCheck(sf::Time& elapsed)
{
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
				movePlayer(RIGHT, elapsed);
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
				movePlayer(LEFT, elapsed);
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
				movePlayer(DOWN, elapsed);
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
				movePlayer(UP, elapsed);
}

float Player::getplayerHealth()
{
	return health;
}

void Player::setplayerHealth(float val)
{
	health = val;
}